﻿
namespace Data.Repository
{
	using System.Linq;
	using System.Collections.Generic;

	public class StateProvinceRepository: IStateProvinceRepository
	{
		private LinqToECommerceDBDataContext _dbContext;

		#region IStateProvinceRepository Members

		public int CreateNewStateProvince(Data.Entity.StateProvince theNewStateProvince)
		{
			throw new System.NotImplementedException();
		}

		public IList<Data.Entity.StateProvince> GetStateProvinceByCountry(int countryID)
		{
			if(countryID <= 0)
			{
				return null;
			}

			List<ECommerce_GetStateProvince_By_CountryResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetStateProvince_By_Country(countryID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.StateProvince>();

				foreach(var sp in lstData)
				{
					var theEntity = new Data.Entity.StateProvince()
					{
						Abbreviation = sp.Abbreviation,
						CountryID = sp.CountryID,
						DisplayOrder = sp.DisplayOrder,
						Name = sp.Name,
						StateProvinceID = sp.StateProvinceID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.City> GetCityByStateProvince(int stateProvinceID)
		{
			if(stateProvinceID <= 0)
			{
				return null;
			}

			List<ECommerce_GetCity_By_StateProvinceResult> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.ECommerce_GetCity_By_StateProvince(stateProvinceID);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.City>();

				foreach(var c in lstData)
				{
					var theEntity = new Data.Entity.City()
					{
						CityDescription = c.CityDescription,
						CityID = c.CityID,
						CityName = c.CityName,
						DisplayOrder = c.DisplayOrder,
						StateProvinceID = c.StateProvinceID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.StateProvince> GetAllStateProvince()
		{
			List<Data.StateProvince> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.StateProvinces.OrderBy(exp => exp.DisplayOrder);
				lstData = data.ToList();
			}

			if(lstData != null)
			{
				var result = new List<Data.Entity.StateProvince>();
				foreach(var sp in lstData)
				{
					var theEntity = new Data.Entity.StateProvince()
					{
						Abbreviation = sp.Abbreviation,
						CountryID = sp.CountryID,
						DisplayOrder = sp.DisplayOrder,
						Name = sp.Name,
						StateProvinceID = sp.StateProvinceID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		public IList<Data.Entity.City> GetAllCity()
		{
			List<Data.City> lstData;

			using(_dbContext = new LinqToECommerceDBDataContext())
			{
				var data = _dbContext.Cities.OrderBy(exp=>exp.DisplayOrder);

				lstData = data.ToList();
			}

			if(lstData != null && lstData.Count > 0)
			{
				var result = new List<Data.Entity.City>();

				foreach(var city in lstData)
				{
					var theEntity = new Data.Entity.City()
					{
						CityDescription = city.CityDescription,
						CityID = city.CityID,
						CityName = city.CityName,
						DisplayOrder = city.DisplayOrder,
						StateProvinceID = city.StateProvinceID
					};

					result.Add(theEntity);
				}

				return result;
			}
			else
			{
				return null;
			}
		}

		#endregion
	}
}
